You should be familiar with creating
stored procedure in MySQL database. But how can you make use of
the stored procedures using Python? In this video you'll learn how to
access stored procedures using Python. Little Lemon restaurant have
a new promotion campaign whereby, they give vouchers to all guests
who spend $50 or more on a meal. To find out which gets qualify for that,
the team need to retrieve the guest names, booking ID's,
bill amount data from the bookings and orders tables in the database. You can complete this task
using a JOIN operation but performing a separate JOIN operation for
each guest is time consuming. Better solution for Little Lemon is to create a stored
procedure they can call using Python. The first step is to write the stored
procedure as a Python in string store in a variable called stored procedure query. To create the procedure type,
create procedure command and then the name of your procedure
which is get customer and bills. strored_procedure_query === CREATE PROCEDURE GetCustomersAndBillAmount(). Next, create begin and end block, BEGIN END. Then tap the logic of your stored
procedure within the block using a SQL select statement. The statement concatenates the required
data from both tables with the use of an energy and for
all the customers who spent $50 more. SELECT bookings.BookingID, CONCAT(bookings.GuestFirstName, bookings.GuestLastName) as CustomerName, Orders.BillAmount, FROM bookings. INNER JOIN, ORDERS ON bookings.BookingID=Orders.BookingID. WHERE BillAmount >= 50. ORDER BY BillAmount DESC;. Don't worry about setting limited
before and after procedure. Name your separate default when working
directly with MySQL to avoid executing the whole procedure as a single statement. But in Python, the stored procedure
passes a single Python string. It can also include
multiple SQL statements. The cursor carries the stored
procedures as a string and its execute module stores
in the MySQL database. So, you don't need to
change the eliminator. You're now ready to execute
the store procedure and store the results into MySQL
database using Python. Call the execute method
from the cursor object and pass procedure to it as an argument. If executed successfully the results
of your procedure should be stored into MySQL database. cursor.execute(stored_procedure_query). Now, you need to retrieve the results
in procedure using Python. First, you need to call or
invoke the procedure. You can do the space in the cursor,
it's called procedure module, which is written as called proc. Pass the name of the procedure to the
module on the cursor object as argument. cursor.callproc("GetCustomersAndBillAmoun- t"). Next, you need to retrieve
the procedure's results. You can make use of Python's built in
next function to complete this task. Invoke stored results module as
an argument to the next function and store them in a Python
variable called results. results= next(cursor.stored_results()). Next function is used to return the next
item from the store results iterator, the entire result set from MySQL service
and buffered in results variable. Now, you can call the fetch all
method on the results variable and save it to the data set. dataset=results.fetchall(). Once the code has been run successfully, the dataset returns to
Python list of tuples. Each tuple is an individual record or
row from the store procedure. You can index the dataset or run the for
loop to print out the results. for data in dataset; print(data). The results of the procedures show that
there is one guest who has spent $50 and more with Little Lemon and
qualify for vouchers. You know how to access stored
procedures using Python. Great work.